//
//  AppDelegate.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "AppDelegate.h"
#import <PgySDK/PgyManager.h>

#import <ShareSDK/ShareSDK.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WXApi.h"
#import "WeiboApi.h"
#import "WeiboSDK.h"
#import <RennSDK/RennSDK.h>

#import "APService.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self setTabBarViewController];
    
    [[PgyManager sharedPgyManager] startManagerWithAppId:@"9026a409c809b37528e8bdeb117655dc"];
    [[PgyManager sharedPgyManager] setEnableFeedback:NO];
    [[PgyManager sharedPgyManager] checkUpdate];
    
    [self shareSDKUsing];
    
    [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                   UIUserNotificationTypeSound |
                                                   UIUserNotificationTypeAlert)
                                       categories:nil];
    
    application.applicationIconBadgeNumber = 0;
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    application.applicationIconBadgeNumber = 0;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    application.applicationIconBadgeNumber = 0;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark JPush
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    [APService handleRemoteNotification:userInfo];
    
    [self showReceiveNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    [self showReceiveNotification:userInfo];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo
  completionHandler:(void (^)())completionHandler {
    
    //NSLog(@"id = %@",identifier);
    
}

- (void)showReceiveNotification:(NSDictionary *)userInfo
{
    //NSLog(@"%@",userInfo);
    NSDictionary *aps = userInfo[@"aps"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"报考大学" message:aps[@"alert"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];

    
//    UILocalNotification *notification = [[UILocalNotification alloc] init];
//    if (notification != nil) {
//        NSDate *now = [NSDate new];
//        notification.fireDate = now;//通知时间
//        notification.repeatInterval = 0;//循环次数，kCFCalendarUnitWeekday一周一次
//        notification.timeZone = [NSTimeZone defaultTimeZone];
//        
//        notification.soundName = UILocalNotificationDefaultSoundName;//声音，可以换成
//        //去掉下面2行就不会弹出提示框
//        notification.alertBody = aps[@"alert"];//提示信息 弹出提示框
//        notification.alertAction = @"打开";  //提示框按钮
//        notification.hasAction = NO; //是否显示额外的按钮，为no时alertAction消失
//        
//        notification.userInfo = userInfo; //添加额外的信息
//        
//        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
//    }
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"报考大学" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//    [alert show];
//    // 图标上的数字减1
//    application.applicationIconBadgeNumber = 0;
//    [[UIApplication sharedApplication] cancelLocalNotification:notification];
}



#pragma mark setup after did launch
- (void)setTabBarViewController{
    
    NSDictionary *textColorDic = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIColor whiteColor],
                                  NSForegroundColorAttributeName,
                                  [UIFont fontWithName:@"DFPWAWAW5-GB" size:14],NSFontAttributeName,nil];
    
    
    UITabBarController *tabBarViewController = (UITabBarController *)self.window.rootViewController;
    NSArray *tabBarArray = tabBarViewController.tabBar.items;
    UITabBarItem *searchTabBarItem = (UITabBarItem *)tabBarArray[0];
    [searchTabBarItem setImage:[[UIImage imageNamed:@"搜索"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [searchTabBarItem setSelectedImage:[[UIImage imageNamed:@"搜索"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    
    [searchTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateSelected];
    [searchTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
    
    UITabBarItem *interActTabBarItem = (UITabBarItem *)tabBarArray[1];
    [interActTabBarItem setImage:[[UIImage imageNamed:@"colleage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [interActTabBarItem setSelectedImage:[[UIImage imageNamed:@"colleage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [interActTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
    [interActTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateSelected];

    UITabBarItem *newsTabBarItem = (UITabBarItem *)tabBarArray[2];
    [newsTabBarItem setImage:[[UIImage imageNamed:@"interact"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [newsTabBarItem setSelectedImage:[[UIImage imageNamed:@"interact"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [newsTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
    [newsTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateSelected];
    
    
    UITabBarItem *meTabBarItem = (UITabBarItem *)tabBarArray[4];
    [meTabBarItem setImage:[[UIImage imageNamed:@"我的按钮"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [meTabBarItem setSelectedImage:[[UIImage imageNamed:@"我的按钮"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [meTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
    [meTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateSelected];
    
    UITabBarItem *newTabBarItem = (UITabBarItem *)tabBarArray[3];
    [newTabBarItem setImage:[[UIImage imageNamed:@"news"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [newTabBarItem setSelectedImage:[[UIImage imageNamed:@"news"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    [newTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateNormal];
    [newTabBarItem setTitleTextAttributes:textColorDic forState:UIControlStateSelected];
}


- (void)shareSDKUsing
{
    [ShareSDK registerApp:@"814d3f849d40"];//字符串api20为您的ShareSDK的AppKey
    
      //添加新浪微博应用 注册网址 http://open.weibo.com
//     [ShareSDK connectSinaWeiboWithAppKey:@"371245107"
//                                appSecret:@"b8d0d3095da8c9a6820b9fb08350e1a4"
//                              redirectUri:@"http://app.mi.com/detail/98265"];
     //当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台 （注意：2个方法只用写其中一个就可以）
//     [ShareSDK  connectSinaWeiboWithAppKey:@"371245107"
//                                 appSecret:@"b8d0d3095da8c9a6820b9fb08350e1a4"
//                               redirectUri:@"http://app.mi.com/detail/98265"
//                               weiboSDKCls:[WeiboSDK class]];
    
//     //添加腾讯微博应用 注册网址 http://dev.t.qq.com
//     [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
//                                   appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
//                                 redirectUri:@"http://www.sharesdk.cn"
//                                    wbApiCls:[WeiboApi class]];
    
    //添加QQ空间应用  注册网址  http://connect.qq.com/intro/login/
    [ShareSDK connectQZoneWithAppKey:@"1104597347"
                           appSecret:@"40fkEPtzWfHMy8k0"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    
    //添加QQ应用  注册网址  http://mobile.qq.com/api/
    [ShareSDK connectQQWithQZoneAppKey:@"1104597347"
                     qqApiInterfaceCls:[QQApiInterface class]
                       tencentOAuthCls:[TencentOAuth class]];
    
    //添加微信应用（注意：微信分享的初始化，下面是的初始化既支持微信登陆也支持微信分享，只用写其中一个就可以） 注册网址 http://open.weixin.qq.com
        [ShareSDK connectWeChatWithAppId:@"wxbe1238a1f6032bb4"
     wechatCls:[WXApi class]];
     //微信登陆的时候需要初始化
     [ShareSDK connectWeChatWithAppId:@"wxbe1238a1f6032bb4"
                            appSecret:@"ee0a1b633a9591829ae98f2dbceaa49b"
                            wechatCls:[WXApi class]];
    
     //添加搜狐微博应用  注册网址  http://open.t.sohu.com
//     [ShareSDK connectSohuWeiboWithConsumerKey:@"SAfmTG1blxZY3HztESWx"
//     consumerSecret:@"yfTZf)!rVwh*3dqQuVJVsUL37!F)!yS9S!Orcsij"
//     redirectUri:@"http://www.sharesdk.cn"];
    
     //添加豆瓣应用  注册网址 http://developers.douban.com
//     [ShareSDK connectDoubanWithAppKey:@"07d08fbfc1210e931771af3f43632bb9"
//     appSecret:@"e32896161e72be91"
//     redirectUri:@"http://dev.kumoway.com/braininference/infos.php"];
    
     //添加人人网应用 注册网址  http://dev.renren.com
     [ShareSDK connectRenRenWithAppId:@"476828"
                               appKey:@"532a58c7646047aebe0cf3a44d1f32d7"
                            appSecret:@"51c493233f124600b1e7b971c4dc1e05"
                    renrenClientClass:[RennClient class]];
    
     //添加开心网应用  注册网址 http://open.kaixin001.com
//     [ShareSDK connectKaiXinWithAppKey:@"358443394194887cee81ff5890870c7c"
//     appSecret:@"da32179d859c016169f66d90b6db2a23"
//     redirectUri:@"http://www.sharesdk.cn/"];
    
     //添加Instapaper应用   注册网址  http://www.instapaper.com/main/request_oauth_consumer_token
//     [ShareSDK connectInstapaperWithAppKey:@"4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"
//     appSecret:@"GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"];
    
     //添加有道云笔记应用  注册网址 http://note.youdao.com/open/developguide.html#app
//     [ShareSDK connectYouDaoNoteWithConsumerKey:@"dcde25dca105bcc36884ed4534dab940"
//     consumerSecret:@"d98217b4020e7f1874263795f44838fe"
//     redirectUri:@"http://www.sharesdk.cn/"];
    
     //添加Facebook应用  注册网址 https://developers.facebook.com
//     [ShareSDK connectFacebookWithAppKey:@"107704292745179"
//     appSecret:@"38053202e1a5fe26c80c753071f0b573"];
    
     //添加Twitter应用  注册网址  https://dev.twitter.com
//     [ShareSDK connectTwitterWithConsumerKey:@"mnTGqtXk0TYMXYTN7qUxg"
//     consumerSecret:@"ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"
//     redirectUri:@"http://www.sharesdk.cn"];
    
    //短信
    [ShareSDK connectSMS];
    //连接邮件
    //[ShareSDK connectMail];
    //连接打印
    ///[ShareSDK connectAirPrint];
    //连接拷贝
    //[ShareSDK connectCopy];
}


- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:self];
}
@end
